import logging
import platform

import shortuuid
from jsonpath import JSONPath
from flask import g
from common import (
    Result,
)
from common.ai import (
    MODEL_GPT35,
    MODEL_GEMINI10,
    MODEL_QIANWEN,
    MODEL_KIMI,
    MODEL_MOCK,
)

from .model.model_proxy import ModelProxy
from .model.base import BaseAiModel


LOGGER = logging.getLogger(__name__)


def _noop(id: int = 0, title: str = ""):
    pass
    # LOGGER.info(f"paragraph => id:{id} title:{title}")

def getModelByName(modelName) -> BaseAiModel:

    return ModelProxy(modelName)


def travelOutlineDict(d: dict, cb: callable = _noop) -> dict:

    def _travel(inputNode):

        idCurrentNode = shortuuid.uuid()
        if "id" in inputNode:
            idCurrentNode = inputNode["id"]
        result = {
            "id": idCurrentNode,
            "title": "",
            "text": inputNode["text"] if "text" in inputNode else "",
            "title": inputNode["title"] if "title" in inputNode else "",
        }
        result["subtitle"] = []
        if "subtitle" in inputNode and type(inputNode["subtitle"]) is list:
            for subDict in inputNode["subtitle"]:
                result["subtitle"].append(_travel(subDict))

        cb(result)
        return result

    return _travel(d)


def findParaById_old(outlineJson: dict, paragraphId: int):
    """废弃"""
    if "id" in outlineJson and outlineJson["id"] == paragraphId:
        return outlineJson["text"], outlineJson["title"]

    if "subtitle" in outlineJson:
        for sub in outlineJson["subtitle"]:
            res = findParaById(sub, paragraphId)
            if res is not None:
                return res


def findParaById(outlineJson: dict, paragraphId: int):
    """废弃"""
    print(outlineJson)

    jp = JSONPath(f"$..subtitle[?(@.id=='{paragraphId}')]")
    res = jp.search(outlineJson)

    if len(res) == 0:
        return None

    return res[0]["title"]
